<?php
class Administrator_Product_Model extends BaseModel{

	public function getProductCatForCombo(){
		$sql = 'SELECT c.id as ' . Constant::COMBOBOX_VALUE .', c.name as ' . Constant::COMBOBOX_TEXT .' FROM productcat as c where c.id != 0 order by c.name';
		return DBHelper::getInstance()->fetchAll($sql);
	}
	public function getProductTypeForCombo(){
		$sql = 'SELECT c.id as ' . Constant::COMBOBOX_VALUE .', c.name as ' . Constant::COMBOBOX_TEXT .' FROM producttype as c order by c.name';
		return DBHelper::getInstance()->fetchAll($sql);
	}
	public function getProductDisplayForCombo(){
		$sql = 'SELECT c.id as ' . Constant::COMBOBOX_VALUE .', c.name as ' . Constant::COMBOBOX_TEXT .' FROM productdisplay as c order by c.name';
		return DBHelper::getInstance()->fetchAll($sql);
	}
	public function getProductManufactureForCombo(){
		$sql = 'SELECT c.id as ' . Constant::COMBOBOX_VALUE .', c.name as ' . Constant::COMBOBOX_TEXT .' FROM productmanufacture as c order by c.name';
		return DBHelper::getInstance()->fetchAll($sql);
	}
	public function countAllProduct(){
		$sql = 'SELECT count(id) as total from product';
		$result = DBHelper::getInstance()->fetchRow($sql);
		if(isset($result))
			return $result['total'];
		return 0;
	}
	public function countAllProductCat(){
		$sql = 'SELECT count(id) as total from productcat';
		$result = DBHelper::getInstance()->fetchRow($sql);
		if(isset($result))
			return $result['total'];
		return 0;
	}
	public function countAllProductDisplay(){
		$sql = 'SELECT count(id) as total from productdisplay';
		$result = DBHelper::getInstance()->fetchRow($sql);
		if(isset($result))
			return $result['total'];
		return 0;
	}
	public function countAllProductDistributor(){
		$sql = 'SELECT count(id) as total from productdistributor';
		$result = DBHelper::getInstance()->fetchRow($sql);
		if(isset($result))
			return $result['total'];
		return 0;
	}
	public function countAllProductManufacture(){
		$sql = 'SELECT count(id) as total from productmanufacture';
		$result = DBHelper::getInstance()->fetchRow($sql);
		if(isset($result))
			return $result['total'];
		return 0;
	}
	public function countAllProductType(){
		$sql = 'SELECT count(id) as total from producttype';
		$result = DBHelper::getInstance()->fetchRow($sql);
		if(isset($result))
			return $result['total'];
		return 0;
	}
	public function getTopNewestProduct($context, $count = null){
		if (!isset($count))
			$count = 10;
		$sql = 'SELECT id, name FROM product order by createtime DESC limit 0,' . $count;
		$result = DBHelper::getInstance()->fetchAll($sql);
		$rs = array();
		foreach ($result as $value) {
			$value['link'] = URLUltil::productLink($context, $value[Product::ID]);
			array_push($rs, $value);
		}
		return $rs;
	}
}